package com.zl.tby.controller;

import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyChangeTrendQuery;
import com.zl.tby.model.TbyChangeTrend;
import com.zl.tby.service.ITbyChangeTrendService;
import com.zl.tby.vo.TbyChangeTrendForm;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * 物种变化趋势控制器
 * @author sunjiaxing
 * @version 1.0
 * @since 2024/8/12 23:34
 */
@Valid
@Controller
@Mapping("/change_trend")
public class TbyChangeTrendController {

    @Inject
    private ITbyChangeTrendService tbyChangeTrendService;

    /**
     * 保存或新增
     * @param form: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/save_or_update", method = MethodType.POST)
    public R<TbyChangeTrend> saveOrUpdate(@Validated TbyChangeTrendForm form) {
        try {
            if (form.getId() == null) {
                return tbyChangeTrendService.add(form);
            }
            return tbyChangeTrendService.update(form);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 删除数据
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/delete", method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return this.tbyChangeTrendService.delete(id);
    }

    /**
     * 根据id查询
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/get", method = MethodType.GET)
    public R<TbyChangeTrend> get(@NotNull Long id) {
        TbyChangeTrend entity = this.tbyChangeTrendService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

    /**
     * 分页查询
     * @param query: 查询参数
     * @return 分页结果
     */
    @Mapping(value = "/page", method = MethodType.GET)
    public R<?> page(@Body TbyChangeTrendQuery query) {
        return R.ok(this.tbyChangeTrendService.page(query));
    }

    /**
     * 列表查询
     * @param query: 查询参数
     * @return 列表结果
     */
    @Mapping(value = "/list", method = MethodType.GET)
    public R<List<TbyChangeTrend>> List(@Body TbyChangeTrendQuery query) {
        return R.ok(this.tbyChangeTrendService.list(query));
    }

}
